/*
描述：网站顶部导航栏组件，包含logo、搜索框和用户操作按钮，根据登录状态显示不同内容
完成人：吴悠
完成时间: 2025/04/27
*/

<script setup lang="ts">
import { useRouter } from 'vue-router';
import { onMounted, ref } from "vue";

const router = useRouter();

const goToLogin = () => {
  router.push('/login');
};

const goToRegister = () => {
  router.push('/register');
};
const goToHome = () => router.push(
    '/home'
);
const goToUser = () => {
  router.push('/user');
};

const goToSearch = () => {
  if (searchInput.value.startsWith('#')) {
    // router.push(`/tag/${searchInput.value.substring(1)}`).then(() => {
    //   window.location.reload(); // 强制刷新页面（类似 F5）
    // });
    router.push({
      path: '/search',
      query: { searchInput: searchInput.value.substring(1) }
    }).then(() => {
      window.location.reload(); // 强制刷新页面（类似 F5）
    });
  } else {
    console.log('Searching for:', searchInput.value);
    router.push({
      path: '/search',
      query: { searchInput: searchInput.value }
    }).then(() => {
      window.location.reload(); // 强制刷新页面（类似 F5）
    });
  }
};

const goToPublishQuestion = () => {
  router.push('/publish-question');
};

const searchInput = ref('');
const isLoggedIn = ref(false);
const userName = ref<string | null>(null);
const avatar = ref<string>('');
const nickname = ref<string| null>('');

onMounted(() => {
  isLoggedIn.value = !!localStorage.getItem('token');
  userName.value = localStorage.getItem('username');
  avatar.value = localStorage.getItem('avatar') || '';
  nickname.value = localStorage.getItem('nickname');
});

const errorHandler = () => true;
</script>

<script lang="ts">
export default {
  name: 'HeaderComponent'
}
</script>

<template>
  <el-header class="header">
    <div class="logo" @click="goToHome">
      <a>
        <img src="../../../public/logo.png" alt="logo" style="height: 40px;width: 40px;">
      </a>
      <span>|</span>
      <span style="color: black">SWPUFORUM</span>
    </div>
    <div v-if="!isLoggedIn" class="right-button">
      <el-input
          v-model="searchInput"
          style="width: 240px"
          placeholder="搜索"
          @keyup.enter="goToSearch"
      >
        <template #prefix>
          <el-icon class="el-input__icon"><svg  xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024"><path fill="currentColor" d="m795.904 750.72 124.992 124.928a32 32 0 0 1-45.248 45.248L750.656 795.904a416 416 0 1 1 45.248-45.248zM480 832a352 352 0 1 0 0-704 352 352 0 0 0 0 704"></path></svg></el-icon>
        </template>
      </el-input>
      <el-button  @click="goToLogin">登录</el-button>
      <el-button  @click="goToRegister" type="primary"  color="#1B75D0">注册</el-button>
    </div>
    <div v-if="isLoggedIn" class="right-button">
      <el-input
          v-model="searchInput"
          style="width: 440px"
          placeholder="搜索"
          @keyup.enter="goToSearch"
      >
        <template #prefix>
          <el-icon class="el-input__icon"><svg  xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024"><path fill="currentColor" d="m795.904 750.72 124.992 124.928a32 32 0 0 1-45.248 45.248L750.656 795.904a416 416 0 1 1 45.248-45.248zM480 832a352 352 0 1 0 0-704 352 352 0 0 0 0 704"></path></svg></el-icon>
        </template>
      </el-input>
      <el-button  @click="goToUser">
        <div class="demo-type">
          <el-avatar :size="27" shape="square" :src="avatar" @error="errorHandler">
            <img
              src="https://gitee.com/jiesenhe/qacommunity_-db/raw/master/avatar/default.webp"
              alt="avatar"
            />
          </el-avatar>
        </div>
        {{nickname}}
      </el-button>
      <el-button  @click="goToPublishQuestion"  color="#E7700D" plain>+创作</el-button>
    </div>
  </el-header>
</template>

<style scoped>
.header {
  background-color: white;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0 20px;
  border-bottom: 2px solid #D6D9DC;
  border-top: 2px solid #E7700D;
  position: fixed;
  z-index: 1000; /* Ensure header stays on top */
}

.logo {
  display: flex;
  align-items: center;
  /* width: 100px; Remove fixed width */
  height: 40px;
  background-color: white;
  cursor: pointer;
  margin-left: 10%;
  /* margin-left: 270px; Remove fixed margin */
}

.logo span {
  background-color: white;
  margin-left: 7px;
  font-size: 20px;
  color: #BCBBBB;
}

.logo span:last-child {
  color: black; /* Keep QAcommunity text black */
}

.right-button {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  background-color: white;
}

.demo-type {
  margin-right: 6px;
  display: flex; /* Ensure avatar aligns correctly */
  align-items: center;
}

/* Add styles for el-input prefix icon if needed */
.el-input__icon {
  display: flex;
  align-items: center;
}
</style>

